//+------------------------------------------------------------------+
//|                                            TradingAssistant.mq4 |
//|                                      智能交易小助手 - 手动交易界面 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Trading Assistant"
#property link      ""
#property version   "1.00"
#property strict

// 包含所有模块
#include "GUIModule.mqh"
#include "TradingModule.mqh"
#include "PriceModule.mqh"

//+------------------------------------------------------------------+
//| 输入参数                                                          |
//+------------------------------------------------------------------+
input int      MagicNumber = 12345;        // EA魔术号
input double   RiskPercent = 2.0;          // 风险百分比

//+------------------------------------------------------------------+
//| 全局变量                                                          |
//+------------------------------------------------------------------+
CGUIModule      *g_GUIModule;       // GUI界面模块
CTradingModule  *g_TradingModule;   // 交易模块
CPriceModule    *g_PriceModule;     // 价格模块

datetime        g_LastUpdate;      // 上次更新时间

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   Print("=== 智能交易小助手界面初始化开始 ===");
   
   // 初始化价格模块
   g_PriceModule = new CPriceModule(Symbol(), 1);
   if(g_PriceModule == NULL)
   {
      Print("错误: 价格模块初始化失败");
      return(INIT_FAILED);
   }
   
   // 初始化交易模块
   g_TradingModule = new CTradingModule(MagicNumber, Symbol(), RiskPercent);
   if(g_TradingModule == NULL || !g_TradingModule.Initialize())
   {
      Print("错误: 交易模块初始化失败");
      return(INIT_FAILED);
   }
   
   // 初始化GUI模块
   g_GUIModule = new CGUIModule(Symbol(), RiskPercent);
   if(g_GUIModule == NULL || !g_GUIModule.CreateInterface())
   {
      Print("错误: GUI模块初始化失败");
      return(INIT_FAILED);
   }
   
   // 初始化更新时间
   g_LastUpdate = TimeCurrent();
   
   Print("=== 智能交易小助手界面初始化完成 ===");
   Print("魔术号: ", MagicNumber);
   Print("风险百分比: ", RiskPercent, "%");
   Print("交易品种: ", Symbol());
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Print("=== 智能交易小助手界面清理开始 ===");
   
   // 清理GUI模块
   if(g_GUIModule != NULL)
   {
      delete g_GUIModule;
      g_GUIModule = NULL;
   }
   
   // 清理交易模块
   if(g_TradingModule != NULL)
   {
      delete g_TradingModule;
      g_TradingModule = NULL;
   }
   
   // 清理价格模块
   if(g_PriceModule != NULL)
   {
      delete g_PriceModule;
      g_PriceModule = NULL;
   }
   
   Print("清理原因: ", GetDeinitReasonText(reason));
   Print("=== 智能交易小助手界面清理完成 ===");
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // 更新价格数据
   if(g_PriceModule != NULL && g_PriceModule.NeedUpdate())
   {
      g_PriceModule.UpdatePrice();
      
      // 更新GUI价格显示
      if(g_GUIModule != NULL)
      {
         PriceData currentPrice = g_PriceModule.GetCurrentPrice();
         g_GUIModule.UpdatePriceDisplay(currentPrice.bid, currentPrice.ask, currentPrice.spreadPoints);
         
         // 更新手数计算
         g_TradingModule.UpdateLotSizeCalculation(g_GUIModule, g_PriceModule);
      }
   }
}

//+------------------------------------------------------------------+
//| 按钮点击事件处理                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
{
   if(id == CHARTEVENT_OBJECT_CLICK && g_GUIModule != NULL && g_TradingModule != NULL)
   {
      string action = g_GUIModule.CheckButtonClick(sparam);
      
      if(action == "BUY")
      {
         g_TradingModule.ExecuteManualBuyOrder(g_GUIModule, g_PriceModule);
         g_GUIModule.ResetButtonState(sparam);
      }
      else if(action == "SELL")
      {
         g_TradingModule.ExecuteManualSellOrder(g_GUIModule, g_PriceModule);
         g_GUIModule.ResetButtonState(sparam);
      }
      else if(action == "CLOSE_BUY")
      {
         g_TradingModule.CloseOrdersByType(OP_BUY);
         g_GUIModule.ResetButtonState(sparam);
      }
      else if(action == "CLOSE_SELL")
      {
         g_TradingModule.CloseOrdersByType(OP_SELL);
         g_GUIModule.ResetButtonState(sparam);
      }
      else if(action == "CLOSE_ALL")
      {
         g_TradingModule.CloseAllOrders();
         g_GUIModule.ResetButtonState(sparam);
      }
      else if(action == "DELETE_PENDING")
      {
         g_TradingModule.DeletePendingOrders();
         g_GUIModule.ResetButtonState(sparam);
      }
   }
}

//+------------------------------------------------------------------+
//| 获取清理原因文本                                                  |
//+------------------------------------------------------------------+
string GetDeinitReasonText(int reason)
{
   switch(reason)
   {
      case REASON_PROGRAM: return "EA被手动移除";
      case REASON_REMOVE: return "EA被删除";
      case REASON_RECOMPILE: return "EA被重新编译";
      case REASON_CHARTCHANGE: return "图表周期或品种改变";
      case REASON_CHARTCLOSE: return "图表被关闭";
      case REASON_PARAMETERS: return "输入参数被改变";
      case REASON_ACCOUNT: return "账户被改变";
      case REASON_TEMPLATE: return "模板被改变";
      case REASON_INITFAILED: return "初始化失败";
      case REASON_CLOSE: return "终端被关闭";
      default: return "未知原因";
   }
}